//
//  BaseModel.m
//  A_Comic
//
//  Created by sos on 16/1/16.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "BaseModel.h"

@implementation BaseModel
- (void)dealloc
{
    [_comic_id release];
    [_lastup release];
    [_artist_name release];
    [_myId release];
    [_cover_url release];
    [_title release];
    [super dealloc];
}
-(instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    
    return self;
    
    
}

+(instancetype)modelWithDic:(NSDictionary *)dic 
{
    //self class 表示当前类
    id m = [[[self class] alloc] initWithDic:dic];
    return [m autorelease];
    
    
}

+(NSMutableArray *)modelHandlerWithArray:(NSArray *)arr
{
    //1.创建空数组 保存处理后的model对象
    NSMutableArray *mArr = [NSMutableArray array];
    //2.遍历传入的数组参数 得到每个字典信息
    for (NSDictionary *dic in arr) {
        //3.使用kvc转换对象
        id m = [[self class] modelWithDic:dic];
        //4.添加到数组
        [mArr addObject:m];
        
    }
    
    return mArr;
    
}


-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
    if ([key isEqualToString:@"id"]) {
        self.myId = value;
    }
    
    
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:_title forKey:@"title"];
    [aCoder encodeObject:_cover_url forKey:@"cover_url"];
    [aCoder encodeObject:_artist_name forKey:@"artist_name"];
    [aCoder encodeObject:_lastup forKey:@"lastup"];
    [aCoder encodeObject:_comic_id forKey:@"comic_id"];
    
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.cover_url = [aDecoder decodeObjectForKey:@"cover_url"];
        self.artist_name = [aDecoder decodeObjectForKey:@"artist_name"];
        self.lastup = [aDecoder decodeObjectForKey:@"lastup"];
        self.comic_id = [aDecoder decodeObjectForKey:@"comic_id"];
        
    }
    return self;
}
@end
